<?php
	echo "<a href=\"javascript:history.go(-1);\">Back</a>";
	if(!isset($_POST['user'])){
		exit;
	}
	require_once("include/lsamp_curl.php");
	/*
	 * Gather information from the submited form
	 */
	$webmin_account['user'] = $_POST['user'];
	$webmin_account['pass'] = $_POST['pass'];

	//Collect url information
	$webmin_base_url = $_POST['base_url'];
	if(substr($webmin_base_url,-1) != "/"){
		$webmin_base_url .= "/";
	}
	$webmin_login_url = $webmin_base_url . 'session_login.cgi';
	$webmin_vs_url = $webmin_base_url . 'virtual-server/';
	$webmin_vs_add_url = $webmin_vs_url . "domain_form.cgi";
	$webmin_vs_setup_url = $webmin_vs_url . "domain_setup.cgi";
	$webmin_vs_edit_url = $webmin_vs_url . "edit_domain.cgi";
	$webmin_vs_delete_url = $webmin_vs_url . "delete_domain.cgi";

	//Parent user information
	$parent_domain = trim($_POST['parent_domain'], ' ');

	//Description
	$description = ($_POST['description'] == "")? date('d/m/Y'): $_POST['description'];
	$alias_description = ($_POST['alias_description'] == "")? date('d/m/Y'): $_POST['alias_description'];



	//Domains to add
	$subdom_domains = array();
	if(isset($_POST['subdom_addlist'])&& $_POST['subdom_addlist'] != "")
	$subdom_domains = explode("\r\n",$_POST['subdom_addlist']);
	$alias_domains = array();
	if(isset($_POST['alias_addlist'])&& $_POST['alias_addlist'] != "")
	$alias_domains = explode("\r\n",$_POST['alias_addlist']);
	$del_domains = array();
	if(isset($_POST['subdom_dellist']) && $_POST['subdom_dellist'] != "")
	$del_domains = explode("\r\n",$_POST['subdom_dellist']);
	//Connect to the webmin to authenticate and retrieve a seesion cookie
	$timeout = (isset($_POST['timeout']))?$_POST['timeout']:'30';
	$vs_manager = new lsamp_cURL(TRUE,$webmin_account['user'] .'_cookies.txt','gzip','',$webmin_vs_url, $timeout);
	$login_data = "user=" . $webmin_account['user'] . "&pass=" . $webmin_account['pass'] . "&save=0&path=/";
	$result = $vs_manager->get($webmin_login_url, $errno, $error);
	$result = $vs_manager->post($webmin_login_url, $login_data, $errno, $error);
	if($errno !== 0){
		die("Error: " . $error);
	}
	//get all domains and its id
	$result = $vs_manager->get($webmin_vs_url, $errno, $error);
	if($errno !== 0){
		die("Error: " . $error);
	}
	preg_match_all('/<a\shref=\'edit_domain.cgi\?dom=([^>]+)\'>([^<]+)<\/a><\/td>\n<td valign=top>([^<]+)<\/td>/', $result, $matches);
	$avail_doms = array();
	$i = 0;
	for(;$i<count($matches[3]);$i++){
		$avail_doms[$matches[2][$i]]['id'] = $matches[1][$i];
		$avail_doms[$matches[2][$i]]['user'] = $matches[3][$i];
	}

	if(!array_key_exists($parent_domain, $avail_doms)){
		echo "<div align='center'><h3><font color='red'>The domain: <font color='blue'>$parent_domain</font> doesn't exist<b><i><font color='blue'></font></i></b></font></h3><br></div>";
		exit;
	}
	// constants used to create random subdomains
	$len = 16;
    $base='ABCDEFGHKLMNOPQRSTWXYZabcdefghjkmnpqrstwxyz123456789';
    $max=strlen($base)-1;

    //Create domains
	if(count($subdom_domains) > 0){

		foreach($subdom_domains as $add_domain){
			$add_domain = trim($add_domain, " \t");
			if($add_domain == "") continue;
			if(strpos($add_domain, ".") == false){
				$dom = $add_domain . "." . $parent_domain;
			}else{
				$dom = $add_domain;
			}
			//setup this domain
			$subdom_post_data = "parentuser=" . $avail_doms[$parent_domain]['user']
						."&prefix_def=prefix_def_0"
						."&template=1"
						."&dir=1"
						."&mail=1"
						."&dns=1"
						."&web=1"
						."&logrotate=1"
						."&webalizer=1"
						."&virt=" . ((isset($_POST['default_ip']))? "0" : "3")
						."&owner=" . $description;
			if(!isset($_POST['default_ip'])){
					$subdom_post_data .= "&sharedip=" . $_POST['shared_ip'];
			}
			$result = $vs_manager->post($webmin_vs_setup_url, $subdom_post_data . "&dom=$dom", $errno, $error);
			if($errno !== 0){
				echo "Errors occured when add $dom : " . $error;
			}else{
				echo "<div align='center'><font color='blue'><h3><b>Response from webmin when <u>creating</u> domain $dom</b></h3></font><br></div>";
				preg_match('/<body.*>((.*[\n])+)<\/body>/m', $result, $matches);
				echo $matches[1];
				// Sleep 2 seconds before creating subdomains.
				sleep(2);
			}

			//generate random subdoamins
			if(isset($_POST['random_flag']) && $_POST['random_flag'] == 'on'){
				$number_of_subdoms = $_POST['number_of_subdoms'];
				$length_of_subdom = $_POST['length_of_subdom'];
				for($i=0;$i<$number_of_subdoms; $i++){
					$subdom='';
					mt_srand((double)microtime()*1000000);
					while (strlen($subdom)<$length_of_subdom)
			      		$subdom .=$base{mt_rand(0,$max)};
				$result = $vs_manager->post($webmin_vs_setup_url, $subdom_post_data . "&dom=$subdom.$dom", $errno, $error);
				if($errno !== 0){
					echo "Errors occured when add $dom : " . $error;
				}else{
					echo "<div align='center'><font color='blue'><h3><b>Response from webmin when <u>creating</u> domain $subdom.$dom</b></h3></font><br></div>";
					preg_match('/<body.*>((.*[\n])+)<\/body>/m', $result, $matches);
					echo $matches[1];
					// Sleep 2 seconds before creating the next subdomain
					sleep(2);
				}

				}
			}
		}
	}
	// add alias domains
	if(count($alias_domains)>0){
		foreach($alias_domains as $alias_domain){
			$alias_domain = trim($alias_domain, " \t");
			if($alias_domain == "") continue;
			if(strpos($alias_domain, ".") == false){
				$dom = $alias_domain . "." . $parent_domain;
			}else{
				$dom = $alias_domain;
			}
			//setup this domain
			$alias_post_data = "&parentuser=" . $avail_doms[$parent_domain]['user']
							."&to=" . $avail_doms[$parent_domain]['id']
							."&owner=" . $alias_description
							."&prefix_def=1"
							."&dir=1"
							."&mail=1"
							."&dns=1"
							."&web=1";
			$result = $vs_manager->post($webmin_vs_setup_url, $alias_post_data . "&dom=$dom", $errno, $error);
			if($errno !== 0){
				echo "Errors occured when add $dom : " . $error;
			}else{
				echo "<div align='center'><font color='Green'><h3><b>Response from webmin when <u>creating alias</u> domain $dom</b></h3></font><br></div>";
				preg_match('/<body.*>((.*[\n])+)<\/body>/m', $result, $matches);
				echo $matches[1];
				// Sleep 2 seconds before creating subdomains.
				sleep(2);
			}

			//generate random subdoamins for this alias
			if(isset($_POST['alias_random_flag']) && $_POST['alias_random_flag'] == 'on'){
				$number_of_alias_subdoms = $_POST['number_of_alias_subdoms'];
				$length_of_alias_subdom = $_POST['length_of_alias_subdom'];
				for($i=0;$i<$number_of_alias_subdoms; $i++){
					$subdom='';
					mt_srand((double)microtime()*1000000);
					while (strlen($subdom)<$length_of_alias_subdom)
			      		$subdom .=$base{mt_rand(0,$max)};
					$result = $vs_manager->post($webmin_vs_setup_url, $alias_post_data . "&dom=$subdom.$dom", $errno, $error);
					if($errno !== 0){
						echo "Errors occured when add $dom : " . $error;
					}else{
						echo "<div align='center'><font color='blue'><h3><b>Response from webmin when <u>creating</u> domain $subdom.$dom</b></h3></font><br></div>";
						preg_match('/<body.*>((.*[\n])+)<\/body>/m', $result, $matches);
						echo $matches[1];
						// Sleep 2 seconds before creating the next subdomain
						sleep(2);
					}

				}
			}
		}
	}

	// Delete domains
	if(count($del_domains) > 0){
		foreach($del_domains as $del_domain){
			$del_domain = trim($del_domain, " \t");
			if($del_domain == "") continue;
			if(strpos($del_domain, ".") == false){
				$dom = $del_domain . "." . $parent_domain;
			}else{
				$dom = $del_domain;
			}
			if(array_key_exists($dom, $avail_doms)){
				$delete_data = "dom=" . $avail_doms[$dom]['id'] . "&only=0" . "&confirm=Yes, Delete It";
				$result = $vs_manager->get($webmin_vs_delete_url . "?" . $delete_data, $errno, $error);
				if($errno !== 0){
					echo "Error: " . $error;
				}else{
					echo "<div align='center'><font color='red'><h3><b>Response from webmin when <u>deleting</u> domain $dom</b></h3></font><br></div>";
					preg_match('/<body.*>((.*[\n])+)<\/body>/m', $result, $matches);
					echo $matches[1];
					// Sleep 2 seconds before deleting the next subdomain.
					sleep(2);
				}
			}else{
				echo "<div align='center'><h3><font color='red'>The domain: <font color='blue'>$dom</font> doesn't exist<b><i><font color='blue'></font></i></b></font></h3><br></div>";
			}
		}
	}

	// Logout the site
	$logout_data = "logout=1";
	$result = $vs_manager->get($webmin_login_url . "?" . $logout_data, $errno, $error);
	if($errno !== 0){
		die("Error: " . $error);
	}


